#include	"delay.h"
#include "config.h"
#include "UART.h"
#include "IR.h"
#include "user_gpio.h"
//#include "IR_NEC.h"
/***************************************/
#define IR_GPIO_NUM 36
bit irScanFlag=0;

void Timer0Init(void)		//125us@22.1184MHz
{
	AUXR |= 0x80;		
	TMOD &= 0xF0;		
	TL0 = 0x33;		
	TH0 = 0xF5;		
	TF0 = 0;		
	TR0 = 1;		
	PT0 = 1;	      
	ET0=1;
}
/***************   *****************/
void	UART_config(void)
{
	COMx_InitDefine		COMx_InitStructure;					
	COMx_InitStructure.UART_Mode      = UART_8bit_BRTx;		
	COMx_InitStructure.UART_BRT_Use   = BRT_Timer1;			
	COMx_InitStructure.UART_BaudRate  = 115200ul;			
	COMx_InitStructure.UART_RxEnable  = ENABLE;				
	COMx_InitStructure.BaudRateDouble = DISABLE;			
	COMx_InitStructure.UART_Interrupt = ENABLE;				
	COMx_InitStructure.UART_Priority    = Priority_0;		
	COMx_InitStructure.UART_P_SW      = UART1_SW_P30_P31;	
	UART_Configuration(UART1, &COMx_InitStructure);		

}
void IR_config()
{
  IR_Init(IR_GPIO_NUM);
	
}
void main()
{
 	timeoutDefine  timeout_1000={1000,0};
	bit led=0;
 Timer0Init();
 UART_config();
 gpio_mode(11,GPIO_PullUp);
 
 EA=1;	
 IR_config();
 printf("ir timer \n");
 printf("v1.0.0 \n");
 while(1)
 {
  if(irScanFlag)
  {
	  irScanFlag=0;
	  IR_NEC();		
	
	}	
  if(t_delay_ms(&timeout_1000))
	{
		 led=!led;
		 GPIO_WriteBit(11,led	);	
	}	
	 IR_printf();
 }	 
}	
/***********************/ 
void tm0_isr() interrupt 1	  //125us
{
 static u8 i=0;
	i++;
 if(i%2==0)
 {
  irScanFlag=1;
 }	
  if(i%8==0) millisecond();
}
